from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, WebDriverException

from utils import WebDriverInstance

# 获取新页面地址；


# 初始化 ChromeDriver
driver = WebDriverInstance().get_driver()

try:
    # 打开初始页面
    driver.get('https://mp.weixin.qq.com/s?__biz=Mzg5NDc0NTIwNA==&mid=2247490573&idx=1&sn=0302b45bca1d4d080ce26b03b15acc27&chksm=c01bb316f76c3a009daaedb99075e607670c84d5da0006e1c54e0ed34a9feb79de7d8809f981&cur_album_id=3406101716922482691&scene=189#wechat_redirect')

    print(f"Old URL: {driver.current_url}")

    # 等待页面元素加载并找到需要点击的按钮或链接
    wait = WebDriverWait(driver, 10)
    print('wait finish...')
    element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'album_read_nav_prev')))
    print('wait element finish...')
    # 点击按钮或链接
    element.click()
    print('click finish...')

    # 获取新地址
    new_url = driver.current_url
    print(f"New URL: {new_url}")

    # 等待新页面加载
    element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'album_read_nav_prev')))
    # 点击按钮或链接
    element.click()
    print('click finish...')

    # 获取新地址
    new_url = driver.current_url
    print(f"New URL: {new_url}")
except WebDriverException as e:
        print(f"WebDriver exception occurred: {e}")
finally:
    # 关闭浏览器
    driver.quit()
